package com.lottery.ruok.lottrey.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.common.Common;
import com.lottery.ruok.lottrey.retrofitModule.response.GetLotteryNoticeResponse;
import com.nex3z.flowlayout.FlowLayout;

import java.util.ArrayList;

import glideHelp.GlideHelper;
import ui.AreaCircle;
import utils.Utils;

public class NoticePageAdapter extends RecyclerView.Adapter<NoticePageAdapter.ViewHolder>{
    public ArrayList<GetLotteryNoticeResponse> datas;
    private Context context;
    private OnItemClickListener mOnItemClickListener = null;
    private int layoutId;

    public NoticePageAdapter(final ArrayList<GetLotteryNoticeResponse> datas, final Context context, int layoutId, boolean isHorizontal) {
        this.datas = datas;
        this.context = context;
        this.layoutId = layoutId;
    }

    public void setDatas(ArrayList<GetLotteryNoticeResponse> datas){
        this.datas = datas;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public ArrayList<GetLotteryNoticeResponse> getDatas(){
        return datas;
    }

    //创建新View，被LayoutManager所调用
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(layoutId, viewGroup,false);
        ViewHolder vh = new ViewHolder(view);
        return vh;
    }

    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {
        GetLotteryNoticeResponse data = datas.get(position);
        viewHolder.mTvName.setText(data.getName());

        if (data.getPrize_num() != null && data.getPrize_num().contains(",")) {
            String[] temp = data.getPrize_num().split(",");
            viewHolder.containerLayout.setVisibility(View.VISIBLE);
            viewHolder.containerLayout.removeAllViews();

            if (data.getCode().equals("xglhc") || data.getCode().equals("jslhc")|| data.getCode().equals("js10flhc")) {
                if (data.getAttached() != null) {
                    if (temp.length == data.getAttached().getColor().size()) {
                        for (int t = 0; t < temp.length; t++) {
                            TextView areaCircle = new TextView(context);
                            switch (data.getAttached().getColor().get(t)) {
                                case "red":
                                    areaCircle.setBackgroundResource(R.mipmap.red_big);
                                    break;
                                case "green":
                                    areaCircle.setBackgroundResource(R.mipmap.green_big);
                                    break;
                                default:
                                    areaCircle.setBackgroundResource(R.mipmap.blue_big);
                                    break;
                            }
                            areaCircle.setText(temp[t]);
                            areaCircle.setGravity(Gravity.CENTER);
                            areaCircle.setTextColor(Color.parseColor("#3f3f3f"));
                            areaCircle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
                            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                            layoutParams.rightMargin = Utils.dip2px(context, 6);
                            areaCircle.setLayoutParams(layoutParams);
                            viewHolder.containerLayout.addView(areaCircle);
                        }
                    }
                }
            } else if("ahk3".equals(data.getCode())||"jiangsuk3".equals(data.getCode())|| Common.isK3(data.getCode())){
                // 快3的图片显示
                for (String s : temp) {
                    int num = Integer.parseInt(s);
                    ImageView imageView = new ImageView(context);
                    imageView.setImageResource(Common.getDiceDrawable(num));
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(Utils.dip2px(context, 20), Utils.dip2px(context, 20));
                    layoutParams.rightMargin = Utils.dip2px(context, 6);
                    imageView.setLayoutParams(layoutParams);
                    viewHolder.containerLayout.addView(imageView);
                }
            } else {
                for (String s : temp) {
                    AreaCircle areaCircle = new AreaCircle(context);
                    Common.setBallBackground(areaCircle, s, data.getCode());
                    areaCircle.setText(s);
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(Utils.dip2px(context, 20), Utils.dip2px(context, 20));
                    layoutParams.rightMargin = Utils.dip2px(context, 6);
                    areaCircle.setLayoutParams(layoutParams);
                    viewHolder.containerLayout.addView(areaCircle);
                }
            }
        } else {
            viewHolder.containerLayout.setVisibility(View.GONE);
        }

        GlideHelper.loadImage(context, data.getImage_url(), viewHolder.logo);
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(v, position);
                }
            }
        });

    }

    //获取数据的数量
    @Override
    public int getItemCount() {
        return datas.size();
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public class ViewHolder extends RecyclerView.ViewHolder {
        FlowLayout containerLayout;
        public TextView mTvName;
        public ImageView logo;
        public ViewHolder(View view){
            super(view);
            mTvName = view.findViewById(R.id.name);
            logo = view.findViewById(R.id.logo);
            containerLayout = view.findViewById(R.id.container);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }
}